import {
    viewInit,
    createOneStaffHtml,
    emptyStaffInput,
    showLoadingDialog,
    hideLoadingDialog,
    showdeleteDialog,
    hidedeleteDialog
} from '../views/userSettingsView.js';
var cw = require("../components/winLocation.js");
import {} from '../base/observer.js';
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

var token, loginName, openid, loginType, deleteCashno, deleteMobile, corp_abbr;
var $stall_box_forms, $iosDialog1;
var errorObject;
$(document).on("pageinit", "#userSettings_page", controllerInit);

// 商户设置控制器初始化
function controllerInit() {
    cw.checkBrowser();
    viewInit();
    initValue();
    queryStaff();
    bindEvent();
    merExtraSearchFunc();
}

// 营业员查询
function queryStaff() {
    var qsParameters = {
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType
    }
    $.post(queryStaff_URL, qsParameters, staffBack)
}

// 营业员查询回调接口
function staffBack(sdata) {
    console.dir(sdata);
    if (sdata.cashier_response && sdata.cashier_response.cashier_info) {
        $stall_box_forms = $("#stall_box_forms");
        $("#stall_box_forms").find(".staff_box").remove(); // 清空营业员
        var infos = sdata.cashier_response.cashier_info;
        var total = sdata.cashier_response.total;
        if (!infos.length) { // 返回一个营业员时不是数组处理
            var ninfos = [];
            ninfos.push(infos);
            infos = ninfos;
        }
        for (var i = 0, l = infos.length; i < l; i++) {
            var oneInfo = infos[i];
            var no;
            if (i < 9) {
                no = "00" + (i + 1);
            } else if (i >= 9 && i < 99) {
                no = "0" + (i + 1);
            }else {
                no = "" + (i + 1);
            }
            oneInfo.no = no;
            createOneStaffHtml(oneInfo);
        }
    } else if (sdata.cashier_response) {
        $("#stall_box_forms").find(".staff_box").remove(); // 清空营业员
    }

}

// 查询手机号等信息
function merExtraSearchFunc() {
    var msParameters = {
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType
    };

    $.post(MerExtraSearch_URL, msParameters, msback);

}

// 查询手机号等信息
function msback(msdata) {
    console.dir(msdata);
    if (msdata.lfq_merextrainfo_response && msdata.lfq_merextrainfo_response.resp_code == "0000") {
        $("#mer_phone").text(msdata.lfq_merextrainfo_response.contact_phone);
    }

}

// 确定收银员添加
function suerAddEvent() {
    $("#suerAdd").on("click", function() {
        $iosDialog1.fadeOut(200);
        postAddStaff()
    });
}


// 请求增加收银员
function postAddStaff() {
    var staffName = $("#staffName").val();
    var staff_phone = $("#staff_phone").val();
    var asParameters = {
        "token": token,
        "bizLicNo": loginName,
        "loginName": loginName,
        "openid": openid,
        "loginType": loginType,
        "cust_name": staffName,
        "phoneNo": staff_phone
    }
    $.post(AddStaff_URL, asParameters, addStaffBack)
}

// 增加收银员回调函数
function addStaffBack(asdata) {
    console.dir(asdata);
    if (asdata.login_response) {
        haslogin(asdata);
        return;
    }
    if (asdata.cashier_response && asdata.cashier_response.resp_code == "0000") {
        errorObject.showErroDialog(asdata.cashier_response.resp_msg);
        queryStaff();
        sendMessage(1);
        emptyStaffInput();
    } else if (asdata.cashier_response && asdata.cashier_response.resp_msg) {
        errorObject.showErroDialog(asdata.cashier_response.resp_msg);
    } else if (asdata.error_response) {
        var errorMsg = asdata.error_response.msg ? asdata.error_response.msg : "添加失败";
        errorObject.showErroDialog(errorMsg);
    }
}

// 商户增加删除营业员给营业员发短信
function sendMessage(delAdd) {
    var parameters;
    if (delAdd == 1) { // 增加
        parameters = getsmsParameters();
    } else {
        parameters = getdelsmsParameters();
    }

    $.post(duanxin_URL, parameters, function() {

    });
}

// 取发送短信参数
function getsmsParameters() {
    var parameters = {};
    parameters.lodinId = $("#staff_phone").val();
    parameters.position = loginType;
    parameters.merName = corp_abbr;
    parameters.sendType = "4"; //发送类型
    //获取url中的openid
    parameters.openid = openid;
    return parameters;
}

/********搜索营业员开始******/
// 营业员查询
function searchStaff() {
    var staffName = $('#search-name').val();
    var staffPhone = $('#search-phone').val();
    var qsParameters = {
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType,
        "name": staffName,
        "phone": staffPhone
    }
    $.post(queryStaff_URL, qsParameters, staffBack)
}

/********搜索营业员结束******/

// 展示是否删除营业弹框
function showIsDeleteStaff(e) {
    var Etarget = $(e.target);
    if (Etarget.attr("data-cashno")) {
        deleteCashno = Etarget.attr("data-cashno");
        deleteMobile = Etarget.attr("data-mobile");
        var deletStaffName = $(Etarget.parent().parent().find(".weui-cell__hd>span")[1]).text();
        $("#delete-staff").text(deletStaffName);
        showdeleteDialog();
    }
}


// 删除营业员
function deleteStaff(e) {
    hidedeleteDialog();
    var dsParameters = {
        "token": token,
        "loginName": loginName,
        "openid": openid,
        "loginType": loginType,
        "cashno": deleteCashno,
        "mobile": deleteMobile
    }

    $.post(deletedStaff_URL, dsParameters, deleteStaffBack);

}

// 删除营业员回调函数
function deleteStaffBack(dsData) {
    if (dsData.login_response) {
        haslogin(dsData);
        return;
    }
    if (dsData.cashier_response && dsData.cashier_response.resp_code) {
        errorObject.showErroDialog(dsData.cashier_response.resp_msg);
        queryStaff();
        sendMessage(0);
    } else {
        errorObject.showErroDialog("删除营业员失败");
    }
}



// 删除发送短信参数
function getdelsmsParameters() {
    var parameters = {};
    parameters.lodinId = deleteMobile;
    parameters.position = loginType;
    parameters.merName = corp_abbr;
    parameters.sendType = "5"; //发送类型
    //获取url中的openid
    parameters.openid = openid;
    return parameters;
}

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}

// 订阅事件\绑定事件
function bindEvent() {
    $("#stall_box_forms").on("touchstart", showIsDeleteStaff)
    $("#delete-yes").on("touchend", deleteStaff);
    suerAddEvent();
    $("#search-staff").on("touchend",function () {
        $("#staff-search_box").show();
    })
    $("#search-no").on("touchend",function () {
        $("#staff-search_box").fadeOut("slow");
    })
    $("#search-yes").on("touchend",function () {
        searchStaff();
        $("#staff-search_box").fadeOut("slow");
    })
    $("#search-all").on("touchend",function () {
        queryStaff();
        $("#search-name").val("");
        $("#search-phone").val("");
    })
}

// 初始化变量
function initValue() {

    getSessionStorage();
    errorObject = new errorDialog();
    $iosDialog1 = $("#addDialog");
    logoutObject = new Logout();
}

// 取本地存储essionStorage
function getSessionStorage() {
    token = sessionStorage.getItem("tltd_token");
    loginName = sessionStorage.getItem("tltd_loginName");
    openid = sessionStorage.getItem("tltd_openid");
    loginType = sessionStorage.getItem("tltd_loginType");
    corp_abbr = sessionStorage.getItem("corp_abbr");
    window.openId = openid;
    console.log(token, loginName, openid, loginType);
}